import Vue from 'vue'
import Router from 'vue-router'
import constantRoutes from './routers'
import { loadMenu } from '@/utils/loadMenu.js'
import store from '@/store'

Vue.use(Router)

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export function setRouterData (level) {
  console.log("setRouterData level:" + level);
}

export function setRouterMenus (menus) {
  //
  let menuslist = loadMenu(store.getters.menus);
  console.log("store.getters.menus",store.state.user.menus)
  for (var i = 0; i < menuslist.length; i++) {
    constantRoutes.push(menuslist[i])
  }
  //
  constantRoutes.push({ path: '*', redirect: '/404', hidden: true }); //404页面最后推入
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router
